using System;
using System.ComponentModel;
using System.ComponentModel.Design.Serialization;
using System.ComponentModel.Design;

namespace Widgetsphere.Generator.Design.Converters
{
  internal class RelationCollectionConverter : TypeConverter
  {
    public RelationCollectionConverter()
    {
    }

    public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
    {
      if (destinationType == typeof(string)) return true;
      return base.CanConvertTo(context, destinationType);
    }

    public override object ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, Type destinationType)
    {
      try
      {
        if (destinationType == typeof(string))
        {
          int count = ((Widgetsphere.Generator.Models.ReferenceCollection)value).Count;
          return count + " item" + (count == 1 ? "" : "s");
        }
      }
      catch (Exception ex) { }
      return null;
    }

  }
}
